/**
 *  请求/spawn服务，服务的数据类型turtlesim::Spawn
*/

#include <ros/ros.h>
#include <turtlesim/Spawn.h>

class TurtleSpawnServiceClientNode {
 public:
  static void init(int argc, char** argv) {
    ros::init(argc, argv, "turtle_spawn_client");
  }

  TurtleSpawnServiceClientNode(std::string name, float theta, float x, float y) {
    // 等待，发现/spawn服务后，创建一个服务的客户端连接名称为/spawn的service.
    ros::service::waitForService("/spawn");
    srv_client_ = nh_.serviceClient<turtlesim::Spawn>("/spawn");
    
    // 初始化请求数据
    turtlesim::Spawn srv_dat;
    srv_dat.request.name  = name;
    srv_dat.request.theta = theta;
    srv_dat.request.x     = x;
    srv_dat.request.y     = y;

    ROS_INFO("Call service to spwan turtle[x:%.6f,y:%.6f,theta:%.6f,name:%s]",
            srv_dat.request.x, srv_dat.request.y, srv_dat.request.theta, srv_dat.request.name.c_str());
    
    // 请求调用
    srv_client_.call(srv_dat);

    // 显示调用结果
    ROS_INFO("Spawn turtle successfully [name:%s]", srv_dat.response.name.c_str());

  }

  ~TurtleSpawnServiceClientNode() { }

 private:
  ros::NodeHandle nh_;
  ros::ServiceClient srv_client_;
};


int main(int argc, char** argv) {
  TurtleSpawnServiceClientNode::init(argc, argv);
  TurtleSpawnServiceClientNode node("turtle2", M_PI/2, 2.0, 2.0);

  return 0;
}
